<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="收获地址id" prop="deliveryAddrId">
      <el-input v-model="dataForm.deliveryAddrId" placeholder="收获地址id"></el-input>
    </el-form-item>
    <el-form-item label="冗余过来的商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="冗余过来的商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品数量" prop="goodsCount">
      <el-input v-model="dataForm.goodsCount" placeholder="商品数量"></el-input>
    </el-form-item>
    <el-form-item label="商品单价" prop="goodsPrice">
      <el-input v-model="dataForm.goodsPrice" placeholder="商品单价"></el-input>
    </el-form-item>
    <el-form-item label="1.pc 2.android 3.ios" prop="goodsChannel">
      <el-input v-model="dataForm.goodsChannel" placeholder="1.pc 2.android 3.ios"></el-input>
    </el-form-item>
    <el-form-item label="订单状态：0新建未支付，1已支付，2已发货，3已收货，4已退款，5已完成" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态：0新建未支付，1已支付，2已发货，3已收货，4已退款，5已完成"></el-input>
    </el-form-item>
    <el-form-item label="订单创建时间" prop="createDate">
      <el-input v-model="dataForm.createDate" placeholder="订单创建时间"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payDate">
      <el-input v-model="dataForm.payDate" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="活动id" prop="activityId">
      <el-input v-model="dataForm.activityId" placeholder="活动id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          goodsId: '',
          deliveryAddrId: '',
          goodsName: '',
          goodsCount: '',
          goodsPrice: '',
          goodsChannel: '',
          status: '',
          createDate: '',
          payDate: '',
          activityId: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          deliveryAddrId: [
            { required: true, message: '收获地址id不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '冗余过来的商品名称不能为空', trigger: 'blur' }
          ],
          goodsCount: [
            { required: true, message: '商品数量不能为空', trigger: 'blur' }
          ],
          goodsPrice: [
            { required: true, message: '商品单价不能为空', trigger: 'blur' }
          ],
          goodsChannel: [
            { required: true, message: '1.pc 2.android 3.ios不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态：0新建未支付，1已支付，2已发货，3已收货，4已退款，5已完成不能为空', trigger: 'blur' }
          ],
          createDate: [
            { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
          ],
          payDate: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          activityId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/activity/orderinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.orderInfo.userId
                this.dataForm.goodsId = data.orderInfo.goodsId
                this.dataForm.deliveryAddrId = data.orderInfo.deliveryAddrId
                this.dataForm.goodsName = data.orderInfo.goodsName
                this.dataForm.goodsCount = data.orderInfo.goodsCount
                this.dataForm.goodsPrice = data.orderInfo.goodsPrice
                this.dataForm.goodsChannel = data.orderInfo.goodsChannel
                this.dataForm.status = data.orderInfo.status
                this.dataForm.createDate = data.orderInfo.createDate
                this.dataForm.payDate = data.orderInfo.payDate
                this.dataForm.activityId = data.orderInfo.activityId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/orderinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'goodsId': this.dataForm.goodsId,
                'deliveryAddrId': this.dataForm.deliveryAddrId,
                'goodsName': this.dataForm.goodsName,
                'goodsCount': this.dataForm.goodsCount,
                'goodsPrice': this.dataForm.goodsPrice,
                'goodsChannel': this.dataForm.goodsChannel,
                'status': this.dataForm.status,
                'createDate': this.dataForm.createDate,
                'payDate': this.dataForm.payDate,
                'activityId': this.dataForm.activityId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
